package com.cmccgz.udacs_landlord_webapp.main.housemanager.function;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.HousePeopleAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.HousePeopleBean;
import com.cmccgz.udacs_landlord_webapp.bean.MobileBean;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/2 16:47
 */

public class HouseCheckOutFragment extends BaseFragment implements HouseFunctionContract.CheckOutView, View.OnClickListener {

    private RecyclerView rv_list;
    private HousePeopleAdapter adapter;
    private HouseFunctionContract.CheckOutPresenter mPresenter;
    private String houseName;
    private String houseId;
    private Button btn_check_out;

    public static HouseCheckOutFragment newInstance() {
        return new HouseCheckOutFragment();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_house_check_out;
    }

    @Override
    protected void initView() {
        initData();
        initTitle();
        findView();
        initRecyclerView();
        showLoadingDialog("获取租客详情");
        mPresenter.getDetail(houseId);
    }

    private void initData() {
        Bundle bundle = getArguments();
        houseName = bundle.getString(HouseFunctionActivity.HOUSE_NAME);
        houseId = bundle.getString(HouseFunctionActivity.HOUSE_ID);
    }

    private void findView() {
        TextView tv_house_name = (TextView) findViewById(R.id.tv_house_name);
        tv_house_name.setText(houseName);
        rv_list = (RecyclerView) findViewById(R.id.rv_list);
        btn_check_out = (Button) findViewById(R.id.btn_check_out);
        btn_check_out.setOnClickListener(this);
    }

    private void initRecyclerView() {
        adapter = new HousePeopleAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(layoutManager);
        rv_list.setAdapter(adapter);
        rv_list.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
    }

    private void initTitle() {
        ViewHelper mViewHelper = ViewHelper.instance(this);
        if (mViewHelper != null) {
            mViewHelper.setTitle("退房");
            mViewHelper.setAllVisable(false);
            mViewHelper.setBackTextVisable(true);
        }
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void getDataSuccess(List<HousePeopleBean> list) {
        dismissLoadingDialog();
        adapter.notifyData(list);
        if (list.size()==0){
            btn_check_out.setEnabled(false);
            showToast("当前房屋没有租客！");
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mContext!=null) {
                        mContext.finish();
                    }
                }
            },2000);
        }else {
            btn_check_out.setEnabled(true);
        }
    }

    @Override
    public void getDataFailed(String errorMsg) {
        dismissLoadingDialog();
        showToast(errorMsg);
    }

    @Override
    public void commitSuccess() {
        dismissLoadingDialog();
        showToast("退房成功！");
        mContext.setResult(Activity.RESULT_OK);
        mContext.finish();
    }

    @Override
    public void commitFailed(String errorMsg) {
        dismissLoadingDialog();
        showToast(errorMsg);
    }

    @Override
    public void setPresenter(HouseFunctionContract.CheckOutPresenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                mContext.finish();
                break;
            case R.id.btn_check_out:
                List<String> selectedPeople = adapter.getSelectedPeople();
                Logger.e("selectedPeople数量：" + selectedPeople.size());
                if (selectedPeople.size() == 0) {
                    showToast("请选择租客！");
                    return;
                }
                List<MobileBean> mobileList = new ArrayList<>();
                for (String mobile : selectedPeople) {
                    mobileList.add(new MobileBean(mobile));
                }
                showLoadingDialog("正在提交数据！");
                mPresenter.commit(mobileList,houseId);
                break;
        }
    }
}
